package managedbean.SABM;

import Beans.StakeholderAccountsBeanRemote;
import Beans.TierManagementBeanRemote;
import Entity.Stakeholder;
import Entity.StakeholderTier;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.faces.application.FacesMessage;  
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;  
import javax.faces.event.ActionEvent;

@ManagedBean(name = "stakeholderAccountsManagerBean")
@SessionScoped
public class StakeholderManagerBean {

    @EJB
     StakeholderAccountsBeanRemote stakeholderAccountBean;
   
     String stakeholderName;
     Integer loyaltyPoints;
     String email;
     String stakeholderTier;
     private String statusMessage;
     
     Stakeholder selectedStakeholder;
     
     
     @EJB
     TierManagementBeanRemote tierManagementBean;

    public StakeholderManagerBean() {}
      
    public void createStakeholder(ActionEvent event) {
                     
        //Stakeholder
        String stakeholder_Name = String.valueOf(getStakeholderName());
        Integer loyalty_points = Integer.valueOf(getLoyaltyPoints());
        String email1 = String.valueOf(getEmail());        
        String stakeholdertier = String.valueOf(getStakeholderTier());
        //String stakeholdertier = "Bronze";
        boolean check = getStakeholderAccountBean().createStakeholderFood(stakeholder_Name, loyalty_points, email1, stakeholdertier);
        if (check == true) {
            statusMessage = (stakeholder_Name + " created successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
      //Send Email to Stakeholder
        Properties props = new Properties();
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.port", "465");
 
	Session session = Session.getInstance(props, new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("foodsage.tcube@gmail.com","3332720twd");
				}
			});
        session.setDebug(true);
 
	try{
 
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("foodsage.tcube@gmail.com"));
		message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email1));
		message.setSubject("FoodSage.sg Account Confirmation");
		message.setText("Dear " + stakeholder_Name + "," + "\n\n Thank You for joining FoodSage.sg! \n\n Your Account has been created successfully." );
 
		Transport.send(message);
 
		System.out.println("Done");
 
	}catch (MessagingException e) {
		throw new RuntimeException(e);
	}
        System.out.print("Email Sent to Stakeholder");
    } else {
            statusMessage = (stakeholder_Name + " already exists");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }
        
    }
    
    
   public List<StakeholderTier> getAvailableTier(){
        return tierManagementBean.getAllTiers();
    }

    
    public List<Stakeholder> getAllStakeholders() {
        return stakeholderAccountBean.getAllStakeholders();
    }
    
    public List<Stakeholder> getSpecificStakeholders(ActionEvent event) {
        String name = String.valueOf(getStakeholderName());
        return stakeholderAccountBean.getSpecificStakeholders(name);
    }
    
    public void deleteStakeholder(Stakeholder stakeholder) {

        
        String s = stakeholder.getName();
        
        boolean check = stakeholderAccountBean.deleteStakeholder(s);
        if (check == true) {
            statusMessage = (s + " deleted successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        } else {
            statusMessage = (s + " got outstanding bill therefore it cannot be deleted");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }
        
    }
    
    public void changeStakeholderTier() {
        System.out.println("Stakeholder: " + this.stakeholderName);
        System.out.println("Tier to Change: " + this.stakeholderTier);
        stakeholderAccountBean.changeStakeholderTier(this.stakeholderTier, this.stakeholderName);
        setStatusMessage(stakeholderName + " changed successfully to this tier: " + stakeholderTier);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
    }
    
    public List<StakeholderTier> getAllTiers() {
        return getTierManagementBean().getAllTiers();
    }
    
    public List<String> getNamesOfAllTiers() {
        List<String> returnList = new ArrayList<String>();
        
        
        for(Object o : tierManagementBean.getAllTiers()){
            StakeholderTier tierEntity = (StakeholderTier)o;
            returnList.add(tierEntity.getName());
        }
        
        return returnList;
        
    }
    
    public List<String> getNamesOfAllStakeholders() {
        List<String> returnList = new ArrayList<String>();
        
        for(Object o : stakeholderAccountBean.getAllStakeholders()){
            Stakeholder stakeholderEntity = (Stakeholder)o;
            returnList.add(stakeholderEntity.getName());
        }
        
        return returnList;
        
    }
    
    public List<String> getNamesOfSpecificStakeholders(String name) {
        List<String> returnList = new ArrayList<String>();
        System.out.println("test" + name);
        
        for(Object o : stakeholderAccountBean.getSpecificStakeholders(name)){
            Stakeholder stakeholderEntity = (Stakeholder)o;
            returnList.add(stakeholderEntity.getName());
        }
        
        return returnList;
        
    }
    


    /**
     * @return the stakeholderAccountBean
     */
    public StakeholderAccountsBeanRemote getStakeholderAccountBean() {
        return stakeholderAccountBean;
    }

    /**
     * @param stakeholderAccountBean the stakeholderAccountBean to set
     */
    public void setStakeholderAccountBean(StakeholderAccountsBeanRemote stakeholderAccountBean) {
        this.stakeholderAccountBean = stakeholderAccountBean;
    }

    /**
     * @return the stakeholderName
     */
    public String getStakeholderName() {
        return stakeholderName;
    }

    /**
     * @param stakeholderName the stakeholderName to set
     */
    public void setStakeholderName(String stakeholderName) {
        this.stakeholderName = stakeholderName;
    }

    /**
     * @return the loyaltyPoints
     */
    public Integer getLoyaltyPoints() {
        return loyaltyPoints;
    }

    /**
     * @param loyaltyPoints the loyaltyPoints to set
     */
    public void setLoyaltyPoints(Integer loyaltyPoints) {
        this.loyaltyPoints = loyaltyPoints;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the stakeholderTier
     */
    public String getStakeholderTier() {
        return stakeholderTier;
    }

    /**
     * @param stakeholderTier the stakeholderTier to set
     */
    public void setStakeholderTier(String stakeholderTier) {
        this.stakeholderTier = stakeholderTier;
    }

    /**
     * @return the tierManagementBean
     */
    public TierManagementBeanRemote getTierManagementBean() {
        return tierManagementBean;
    }

    /**
     * @param tierManagementBean the tierManagementBean to set
     */
    public void setTierManagementBean(TierManagementBeanRemote tierManagementBean) {
        this.tierManagementBean = tierManagementBean;
    }

    /**
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }


        /**
     * @return the selectedStakeholder
     */
    public Stakeholder getSelectedStakeholder() {
        return selectedStakeholder;
    }

    /**
     * @param selectedStakeholder the selectedStakeholder to set
     */
    public void setSelectedStakeholder(Stakeholder selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
        System.out.println(">>>>>> "+this.selectedStakeholder.getName());
    }

    
   



 }  
